package team.niit.ict.mydoctor;

import team.niit.ict.adapter.FragAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements TabListener {

	private ViewPager viewPager;
	private FragAdapter fragAdapter;
	private ActionBar actionBar;
	private String[] list_name_tab = { "Khám Bệnh", "Đánh Dấu", "Thường Gặp",
			"Phòng Khám" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		viewPager = (ViewPager) findViewById(R.id.viewPager);
		fragAdapter = new FragAdapter(getSupportFragmentManager());
		viewPager.setAdapter(fragAdapter);

		actionBar = getActionBar();

		actionBar.setHomeButtonEnabled(false);
		actionBar.setDisplayShowTitleEnabled(true);


		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		for (String name_tab : list_name_tab) {
			Tab t = actionBar.newTab().setText(name_tab);
			t.setTabListener(this);
			actionBar.addTab(t);
		}

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				actionBar.setSelectedNavigationItem(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.search, menu);
		return super.onCreateOptionsMenu(menu);
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == R.id.search){
			Intent intent = new Intent(this, Search.class);
			startActivity(intent);
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

}
